//初始化导航栏
function onLoad(){
	initNav();
}

//检查填写的商品信息并提交
function checkAndSubmit(gid){
	var form=document.getElementById("form-goodsInfo");
	var data=new FormData(form);
	var name=data.get("name");
	var price=data.get("price");
	var des=data.get("des");
	var img=data.get("img");
	var uploader=getCookie("username");
	//未登录，转到登录
	if(uploader==""){
		window.location="/login";
		return;
	}
	data.append("uploader",uploader);
	console.log(form);

	if(name=='' || price=='' || des=='' || (img.name=='' && gid=='-1')){
		alert("请填写完整后再提交");
	}else{
		data.append("gid",gid);
		var url;
		if(gid=='-1'){
			url="/upload_new";
		}else{
			url="/upload_update";
		}
		$.ajax({
			type: 'post',
			url: url,
			data: data,
			cache: false,
			processData: false,
			contentType: false,
			success: function(){
				alert("提交成功");
				window.location="/";
			},
			fail: function(){
				alert("提交失败");
			},
			error: function(){
				alert("服务器内部错误");
			}
		});
	}
}

//更新信息时预览图片
function setPreview(context){
	var reader=new FileReader();
	var img;
	if(context==0){
		img=document.getElementById("fileInUp").files[0];
	}else{
		document.getElementById("preview").style.display="block";
		img=document.getElementById("fileInNew").files[0];
	}
	reader.onload=function(e){
		var pre=document.getElementById("preview");
		pre.src=e.target.result;
	}
	reader.readAsDataURL(img);
}

//取消修改
function cancelEdit(){
	window.location="/myinfo";
}